﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Ensynch.FIM;
using Microsoft.ReportingServices.DataProcessing;

namespace FimDataProcessingExtension
{
    /*
    * Because IDataParameterCollection is primarily an IList,
    * the sample can use an existing class for most of the implementation.
    * In this sample, we use ArrayList. 
    */

    public sealed class FimDataParameterCollection : ArrayList, IDataParameterCollection
    {
        // Create a logger for use in this class.
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
                System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public object this[string index]
        {
            get
            {
                return this[IndexOf(index)];
            }
            set
            {
                this[IndexOf(index)] = value;
            }
        }

        public int Add(object value)
        {
            FimTools.LogTrace(false);

            return ((IDataParameterCollection)this).Add((IDataParameter)value);
        }

        int IDataParameterCollection.Add(IDataParameter parameter)
        {
            #if TRACE
            FimTools.LogTrace(false);
            log.Debug("[FimDataParameter.ctor] " + parameter.ParameterName + " = " + parameter.Value);
            #endif

            if (parameter.ParameterName == null)
            {
                throw new ArgumentException("parameter must be named");
            }

            int result = 0;

            // The FimDataParameter doesn't like null values.
            object value = parameter.Value == null ? string.Empty : parameter.Value;
            if (IndexOf(parameter.ParameterName) >= 0)
            {
                this[parameter.ParameterName] = value;
            }
            else
            {
                result = base.Add(new FimDataParameter(parameter.ParameterName, value));
            }

            return result;
        }

        public void AddAll(Dictionary<string, object> dictionary)
        {
            FimTools.LogTrace(false);

            foreach (KeyValuePair<string, object> item in dictionary)
            {
                ((IDataParameterCollection)this).Add(new FimDataParameter(item.Key, item.Value));
            }
        }

        public Dictionary<string, object> ToDictionary()
        {
            FimTools.LogTrace(false);

            Dictionary<string, object> result = new Dictionary<string, object>();
            StringBuilder msg = new StringBuilder("[ToDictionary]");
            foreach (IDataParameter param in this)
            {
                msg.AppendFormat("\r\n{0} = {1}", param.ParameterName, param.Value);
                if (!result.ContainsKey(param.ParameterName))
                {
                    result.Add(param.ParameterName, param.Value);
                }
                else
                {
                    msg.Append(" [SKIPPED(DUPLICATE)]");
                }
            }
            log.Debug(msg.ToString());
            return result;
        }

    }
}
